﻿using Ledict.ConditionExtract.Base;
using Ledict.ConditionExtract.BLL;
using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ledict.ConditionExtract.Frm.NodeTree
{
    public partial class DataReturnConfig : BaseForm
    {
        NewIDBLL newid = new NewIDBLL();
        public ReturnMode rm = new ReturnMode();
        ReturnModeBLL returnModeBll = new ReturnModeBLL();
        public bool IsAdd { get; set; }
        public string state = string.Empty;
        public DataReturnConfig()
        {
            InitializeComponent();
        }

        private void MoveState_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(rm.RM_Code))
            {
                txbRMCode.Text = rm.RM_Code;
                txbRMCodeZH.Text = rm.RM_CodeZH;
                cbxIsZhi.SelectedIndex = rm.RM_IsValue;
                txbDataPinJie.Text = rm.RM_JoinMode;
            }
            else
            {
                cbxIsZhi.SelectedIndex = 1;
                txbDataPinJie.Enabled = false;
            }
            if (state == "添加")
            {
                txbRMCode.Text = "Default";
                txbRMCodeZH.Text = "默认集合方式";
            }
        }


        private void btnCanel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// 确定按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txbRMCode.Text.Trim()))
            {
                MessageBox.Show("数据返回编号不能为空！");
                txbRMCode.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txbRMCodeZH.Text.Trim()))
            {
                MessageBox.Show("中文名称不能为空！");
                txbRMCodeZH.Focus();
                return;
            }
            if (cbxIsZhi.SelectedIndex == 0)
            {
                if (cbxIsZhi.SelectedIndex == 0 && string.IsNullOrEmpty(txbDataPinJie.Text.Trim()))
                {
                    MessageBox.Show("数据拼接方式不能为空！");
                    txbDataPinJie.Focus();
                    return;
                }
                if (txbDataPinJie.Text.Length > 2)
                {
                    MessageBox.Show("数据拼接方式最大为2个字符！");
                    txbDataPinJie.Focus();
                    return;
                }
            }
            if (rm != null)
            {
                rm.RM_Code = txbRMCode.Text.Trim();
                rm.RM_CodeZH = txbRMCodeZH.Text.Trim();
                rm.RM_IsValue = cbxIsZhi.SelectedIndex;
                rm.RM_UpdateTime = DateTime.Now;
            }
            else
            {
                rm = new ReturnMode() { RM_Code = txbRMCode.Text.Trim(), RM_CodeZH = txbRMCodeZH.Text.Trim(), RM_IsValue = cbxIsZhi.SelectedIndex, RM_UpdateTime = DateTime.Now };
            }
            if (cbxIsZhi.SelectedIndex == 0)
            {
                rm.RM_JoinMode = txbDataPinJie.Text.Trim();
            }
            else
            {
                rm.RM_JoinMode = "";
            }
            if (!IsAdd&&!string.IsNullOrEmpty(rm.RM_ID)&&state=="修改")
            {
                if (returnModeBll.ReturnModeUpdate(rm))
                {
                    MessageBox.Show("修改成功！");
                }
                else
                {
                    MessageBox.Show("修改失败！");
                }
            }
            else if (!IsAdd && !string.IsNullOrEmpty(rm.RM_ID) && state == "添加")
            {
                if (returnModeBll.ReturnModeAdd(rm))
                {
                    MessageBox.Show("新增成功！");
                }
                else
                {
                    MessageBox.Show("新增失败！");
                }
            }
            else
            {
                rm.RM_ID = Snowflake.GetId().ToString(); 
            }
            DialogResult = DialogResult.Yes;
            this.Close();

        }

        /// <summary>
        /// 是否值方式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbxIsZhi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxIsZhi.SelectedIndex == 0)
            {
                txbDataPinJie.Enabled = true;
            }
            else if (cbxIsZhi.SelectedIndex == 1)
            {
                txbDataPinJie.Enabled = false;
                txbDataPinJie.Text = "";
            }

        }


    }
}
